<?php
// +----------------------------------------------------------------------
// | 图片控制器
// +----------------------------------------------------------------------

class image_controller extends base_controller {

	public function _init(){
		
	}

	//图片详情页
	public function detail(){
		$name = $_GET['name'];
		$id = (int)$_GET['id'];

		if($id<=0) redirect404();

		//统计
		//$this->image->where("id='$id'")->save(array('view'=>array('exp','view+1')));

		//================= 检查静态页缓存 ======================
		$cachefile = $this->getImageCacheFile('detail',$id,$name);
		if(is_file($cachefile)){
			//缓存存在
			if((time() - filemtime($cachefile)) < C('detail_cache_time')) {
				echo file_get_contents($cachefile);
				exit();
			}
		}

		//================= 重新生成缓存 ======================
		$info = $this->image->where("id='$id'")->find();
		if(empty($info) || ($name!=name2dir($info['name']))) {
			redirect404();
		}

		$content = $this->_detail($id,true);
		if(empty($content)){
			redirect404();
		} else {
			echo $content;
		}
	}

	//获取图片详情页HTML
	public function _detail($id,$html=false){
		if(!is_numeric($id)) return '';

		//左边和底部公共数据
		$this->image->init();
		$this->getLeftData();
		$this->getFooterData();

		$var = array();		

		//图片详情
		$var['info'] = $info = $this->image->getInfo($id,true);

		//当前位置
		$var['position'] = $this->category->getPositionHtml($info['catid2']);

		//6张同分类图片
		$map = array();
		$map['catid2'] = $info['catid2'];
		$field = 'id,name,catid1,catid2,img,view,download,addtime,updatetime';
		$rows = $this->image->getList($map, 6, $field, 'rand()', '', true);
		if(empty($rows)){
			$map = array();
			$map['catid1'] = $info['catid1'];
			$rows = $this->image->getList($map, 6, $field, 'rand()', '', true);
		}elseif(count($rows)<6){
			$map = array();
			$map['catid1'] = $info['catid1'];
			$rows2 = $this->image->getList($map, 6-count($rows), $field, 'rand()', '', true);
			$rows = array_merge($rows,$rows2);
		}
		$var['list_relate'] = $this->image->format($rows,true);

		//相关图片
		$rows = $this->image->getList('', 9, $field, 'rand()', '', true);
		$var['list_relate2'] = $this->image->format($rows,true);

		//底部推荐头条
		$arr = array(12=>'Celebrities', 10=>'Cars', 13=>'Movies');
		$tops = $map = array();
		foreach($arr as $k=>$v){
			$map['catid1'] = $k;
			$rows = $this->image->getList($map, 5, $field, 'rand()', 'catid2', true);
			$tmp = array();
			$tmp['name'] = $v;
			$tmp['url'] = url('category',$k);
			$tmp['lists'] = $this->image->format($rows,true);
			$tops[] = $tmp;
		}
		$var['tops'] = $tops;

		//分辨率
		$piexls = C('piexl');
		foreach($piexls as $k=>$v){
			foreach($v as $k2=>$v2){
				$row = array();
				$row['name'] = str_replace("_", "x", $v2);
				$row['url'] = imgurl($info['id'],$v2);
				$v[$k2] = $row;
			}
			$piexls[$k] = $v;
		}
		$var['piexls'] = $piexls;

		//热门关键词
		$name = $info['name'];
		if(strlen($info['name'])<4) {
			$name = strlen($info['cat2_name'])>=4 ? $info['cat2_name'] : $info['cat1_name'];
		}
		$rows = $this->keywords->getHotKeywords(20,$name);
		$var['hot_keywords'] = $this->keywords->format($rows);

		$this->assign('seo_title',$info['name'].' Android & iPhone & iPad Wallpapers,'.$info['name'].' Wallpapers & Pictures Free Download');
		$this->assign('seo_keywords','Download '.$info['name'].' Android & iPhone & iPad Wallpapers');
		$this->assign('seo_description','Free '.$info['name'].' Android & iPhone & iPad Wallpapers download, download free High Resolution, HD, Widescreen '.$info['name'].' wallpapers and pictures.');

		$this->assign($var);
		$content = $this->fetch('image_detail');
		if($content){
			$cachefile = $this->getImageCacheFile('detail',$info['id'],name2dir($info['name']));
			if(!is_dir(dirname($cachefile))) mk_dir(dirname($cachefile),0777);
			file_put_contents($cachefile, $content);
			@chmod($cachefile, 0777);
		}
		return $content;
	}

	//生成全部图片页
	public function detailAll(){
		$this->chkCmd();
		$count = $this->image->where("status=1")->count();

		$time1 = time();
		echo "\n==================  ".date('Y-m-d H:i:s')." detail create html!\n";
		for($i=0;$i<=$count;$i=$i+100){
			$rows = $this->image->field("id,name")->where("status=1")->order("id asc")->limit("$i,100")->findAll();
			if($rows){
				foreach ($rows as $v) {
					$this->_detail($v['id'],true);
					echo $v['id']." ".$v['name']."\n";
				}
			}
		}
		$time2 = time()-$time1;
		echo "\n==================  run time: $time2 seconds  ==================\n";
	}

	//图片像素页
	public function pixel(){
		dump($_GET);
	 exit;
		$var = array();


		$this->assign($var);
		$this->display();
	}


	//彻底删除图片数据和本地全部附件
	public function imageDelete() {
		exit();
		$id = $_GET['id'];
		if (!is_numeric($id))
			exit('id not number');
		$f1 = imgurl($id,'',true);
		$num = $this->image->where("id='{$id}'")->delete();
		if($num>0){
			$lists = glob(dirname($f1)."/{$id}_*.jpg");
			$lists[] = $f1;
			foreach($lists as $v){
				unlink($v);
			}
		}
		echo "image_id:".$num." delete successful!";
	}

}